package com.kaigejava.rulesengine.engine.core.kie;

import org.kie.api.definition.rule.Rule;
import org.kie.api.event.rule.AfterMatchFiredEvent;
import org.kie.api.event.rule.DefaultAgendaEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 规则匹配监听器
 * @author kaigejava
 */
public class TrackingAgendaEventListener extends DefaultAgendaEventListener {

	private static Logger logger = LoggerFactory.getLogger(TrackingAgendaEventListener.class);

	@Override
	public void afterMatchFired(AfterMatchFiredEvent event) {
		Rule rule = event.getMatch().getRule();
		String ruleName = rule.getName();
		Map<String, Object> ruleMetaDataMap = rule.getMetaData();
        StringBuilder builder = new StringBuilder("Rule fired: " + ruleName + ",rule id:" + rule.getId()) ;
        if (ruleMetaDataMap.size() > 0) {
            builder.append("\n  With [" + ruleMetaDataMap.size() + "] meta-data:");
            for (String key : ruleMetaDataMap.keySet()) {
                builder.append("\n    key=" + key + ", value=" + ruleMetaDataMap.get(key));
            }
        }
        logger.info(builder.toString());
	}
}
